package com.magina.mq.controller;

import com.magina.common.response.Response;
import com.magina.mq.rabbit.exchange.RabExchangeSend;
import com.magina.mq.rabbit.queue.RabQueueSender;
import com.magina.mq.rabbit.queue.RabQueueWithComfirmSender;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

//import com.magina.mq.rabbit.exchange.RabExchangeWithComfirmSend;

@Api(value = "/testRabbitMq", description = "测试RabbitMq接口")
@RestController
@RequestMapping("/testRabbitMq")
@Slf4j
public class RabbitMQTestController extends BasicAction {


    @Autowired
    private RabQueueSender rabQueueSender;
    @Autowired
    private RabExchangeSend rabExchangeSend;

//    @Autowired
//    private RabExchangeWithComfirmSend rabExchangeWithComfirmSend;

//    @Autowired
//    private RabQueueReceiver receiver;


    @ApiOperation(value = "模拟rabbit_mq发送到Queue", notes = "模拟rabbit_mq发送到Queue")
    @PostMapping("/sendQueueMsg")
    public Response sendQueueMsg(HttpServletRequest request, HttpServletResponse response) {
        for (int i = 0; i < 11; i++) {
            rabQueueSender.send(i);
        }
        log.info("模拟rabbit_mq发送到Queue发送完毕!");
        return  Response.succeed("模拟rabbit_mq发送到Queue发送完毕!");
    }

    @ApiOperation(value = "模拟mq发送direct模式exchange的mq消息", notes = "{\"type\":2}")
    @PostMapping("/sendDirectExchangeMsg")
    public Response sendMsg_mq_02(@RequestBody Map map) throws InterruptedException {
        Integer type = Integer.valueOf(map.get("type").toString());
        for (int i = 0; i < 5; i++) {
            Thread.sleep(1000L);
            rabExchangeSend.sendWithExchange(type,i);
        }
        log.info("模拟mq发送direct模式exchange的mq消息!");
        return  Response.succeed("模拟mq发送direct模式exchange的mq消息");
    }


    @ApiOperation(value = "模拟mq发送topic模式exchange的mq消息", notes = "")
    @PostMapping("/sendTopicExchangeMsg")
    public Response sendTopicExchangeMsg() throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            Thread.sleep(1000L);
            rabExchangeSend.sendProductLogWithExchange(i);
            rabExchangeSend.sendOrderLogWithExchange(i);
        }
        log.info("模拟mq发送topic模式exchange的mq消息!");
        return  Response.succeed("模拟mq发送topic模式exchange的mq消息");
    }

    @Autowired
    private RabQueueWithComfirmSender rabQueueWithComfirmSender;

    @ApiOperation(value = "模拟rabbit_mq发送到Queue(事务控制_带回调)", notes = "{\"type\":2}")
    @PostMapping("/sendQueueMsgWithComfirm")
    public Response sendQueueMsgWithComfirm(@RequestBody Map map) {
        Integer type = Integer.valueOf(map.get("type").toString());
//        rabExchangeWithComfirmSend.sendWithExchange(type, 1);
//        rabExchangeWithComfirmSend.sendOrderLogWithExchange(2);
//        rabExchangeWithComfirmSend.sendProductLogWithExchange(3);
        rabQueueWithComfirmSender.send(1);
        log.info("模拟rabbit_mq发送到Queue(事务控制_带回调)!");
        return Response.succeed("模拟rabbit_mq发送到Queue(事务控制_带回调)!");
    }

//    @ApiOperation(value = "模拟mq接受", notes = "模拟mq接受")
//    @PostMapping("/receiveMsg_mq")
//    public Response receiveMsg_mq(HttpServletRequest request, HttpServletResponse response) {
//        receiver.process();
//        log.info("mq发送完毕!");
//        return  Response.succeed("send success!");
//    }


}
